﻿/**
 * Copyright (C) 2004-2009 D3Systems, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace OMGGateways
{
    public class GWController
    {
        /// <summary>
        /// An array containing all the supported Gateway Interfaces
        /// </summary>
        List<GWInterface> listGateway;

        // Event Logger for Debugging Purposes
        /// <summary>
        /// Reference to the Event Log object used to track and debug the application.
        /// </summary>
        System.Diagnostics.EventLog logger;


        /// <summary>
        /// Singleton instance of the GWController
        /// </summary>
        static GWController instance;

        /// <summary>
        /// Method for retrieving the singleton instance of the GW Controller
        /// </summary>
        /// <returns></returns>
        public static GWController getInstance()
        {
            if (instance == null)
            {
                instance = new GWController();
            }

            return instance;
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        public GWController()
        {
            listGateway = new List<GWInterface>();
        }

        /// <summary>
        /// Set the Logger for Debugging
        /// </summary>
        /// <param name="logger"></param>
        public void SetLogger(System.Diagnostics.EventLog logger)
        {
            this.logger = logger;
        }

        /// <summary>
        /// Register a Login Detail.
        /// </summary>
        /// <param name="loginDetails"></param>
        public void RegisterGateway(LoginDetails loginDetails)
        {
            try
            {
                // Use Dynamic Classloading to load the Gateway Interface as defined by the LoginDetail
                System.Reflection.Assembly assembly = System.Reflection.Assembly.Load(loginDetails.AssemblyName);
                GWInterface gw = (GWInterface)assembly.CreateInstance(loginDetails.ClassName);

                if (gw != null)
                {
                    // Add to the Array of Gateway Interfaces
                    listGateway.Add(gw);

                    // Initialize the Gateway Interface
                    gw.Init(loginDetails, logger);
                }
            }
            catch (Exception ex)
            {
                logger.WriteEntry("Error Registering Gateway: " + ex.ToString());
            }
        }

        /// <summary>
        /// Method used to login to all Registered Gateways
        /// </summary>
        public void LoginAll()
        {
            logger.WriteEntry("Found " + listGateway.Count() + " Registrations: Logging in them now ..." );

            foreach(GWInterface gw in listGateway)
            {
                gw.Login();
            }
        }
    }
}
